﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变
{
    public partial class user_rentCarForm : Form
    {
        /// <summary>
        /// 设置窗体的Region
        /// </summary>
        public void SetWindowRegion()
        {
            GraphicsPath FormPath;
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, 50);
            this.Region = new Region(FormPath);

        }
        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();

            // 左上角
            path.AddArc(arcRect, 180, 90);

            // 右上角
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);

            // 右下角
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();//闭合曲线
            return path;
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x84:
                    base.WndProc(ref m);
                    if ((int)m.Result == 0x1)
                        m.Result = (IntPtr)0x2;
                    return;
            }

            base.WndProc(ref m);
        }
        public user_rentCarForm()
        {
            InitializeComponent();
        }
        public string carNumber { get; set; }
        public string price { get; set; }
        public string opername { get; set; }

        public string carName { get; set; }

        private void user_rentCarForm_Load(object sender, EventArgs e)
        {
            txtCarnumber.Text = this.carNumber;
            SetWindowRegion();
        }

       

        private void uiDatetimePicker2_TextChanged(object sender, EventArgs e)
        {

            TimeSpan tp = uiDatetimePicker2.Value.Subtract(uiDatetimePicker1.Value);
            string days = tp.Days.ToString();
            labMoney.Text = (Convert.ToDecimal(days) * Convert.ToDecimal(this.price)).ToString();
       
        }
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            string sql = "insert into carRents(price,begindate,returntime,rentflag,CNumber,opername,carname,isPay) " +
                "values(@price,@begindate,@returntime,0,@CNumber,@opername,@carname,'False')";
            SqlParameter[] ps = new SqlParameter[]
                {
                    new SqlParameter("@price", labMoney.Text),
                    new SqlParameter("@begindate",uiDatetimePicker1.Text),
                    new SqlParameter("@returntime",uiDatetimePicker2.Text),
                    //new SqlParameter("@rentflag",0),
                    new SqlParameter("@CNumber",this.carNumber),
                    new SqlParameter("@opername",this.opername),
                    new SqlParameter("@carname",this.carName)
            };
            
            //string sql = $"insert into carRents(price,begindate,returntime,rentflag,CNumber,opername) " +
            //"values('{labMoney.Text}'," +
            //"'{uiDatetimePicker1.Text}'," +
            //"'{uiDatetimePicker2.Text}'," +
            //"{0}," + "'{this.carNumber}'," +"'{this.opername}');";
            int res = DBHelper.ExecuteNonQuery(sql,ps);
           
            if(res >0)
            {
                this.ShowSuccessTip("下单成功");
                //修改汽车的状态为使用中
                string sql2 = "update CarInfo set CStatus = 'False' where CNumber=@CNumber";
                SqlParameter p = new SqlParameter("@CNumber", this.carNumber);
                DBHelper.ExecuteNonQuery(sql2, p);
                this.DialogResult = DialogResult.OK;

            }
            else
            {
                this.ShowErrorTip("下单失败！");
            }
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            string sql = "insert into carRents(price,begindate,returntime,rentflag,CNumber,opername,carname,isPay) " +
                "values(@price,@begindate,@returntime,0,@CNumber,@opername,@carname,'False')";
            SqlParameter[] ps = new SqlParameter[]
                {
                    new SqlParameter("@price", labMoney.Text),
                    new SqlParameter("@begindate",uiDatetimePicker1.Text),
                    new SqlParameter("@returntime",uiDatetimePicker2.Text),
                    //new SqlParameter("@rentflag",0),
                    new SqlParameter("@CNumber",this.carNumber),
                    new SqlParameter("@opername",this.opername),
                    new SqlParameter("@carname",this.carName)
            };

            //string sql = $"insert into carRents(price,begindate,returntime,rentflag,CNumber,opername) " +
            //"values('{labMoney.Text}'," +
            //"'{uiDatetimePicker1.Text}'," +
            //"'{uiDatetimePicker2.Text}'," +
            //"{0}," + "'{this.carNumber}'," +"'{this.opername}');";
            int res = DBHelper.ExecuteNonQuery(sql, ps);

            if (res > 0)
            {
                this.ShowSuccessTip("下单成功");
                //修改汽车的状态为使用中
                string sql2 = "update CarInfo set CStatus = 'False' where CNumber=@CNumber";
                SqlParameter p = new SqlParameter("@CNumber", this.carNumber);
                DBHelper.ExecuteNonQuery(sql2, p);
                this.DialogResult = DialogResult.OK;

            }
            else
            {
                this.ShowErrorTip("下单失败！");
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
